<?php
    header("Content-type:text/html;charset=utf-8");
    // 1.连接数据库
    $mysqli = new mysqli("localhost","root","root","sten");
    // 2.判断成功连接数据库
    echo $mysqli->connect_errno;
    if($mysqli->connect_errno != 0){
        die();
    }
    // 3.接收post请求传递过来的对象 {data:[{name:,age:},{},{},{}]}
    // print_r($_POST);
    $data = $_POST["data"]; //[{name:,age:},{},{},{}]
    //4.循环数组的到每个对象
    for($i = 0 ;$i < count($data);$i++){
        
        $n = $data[$i]["name"]; //陈晓芳
        $a = $data[$i]["age"]; //18
        $p = $data[$i]["phone"]; //150
        $h = $data[$i]["hobby"];
        $d = $data[$i]["describe"];

        // 5.在数据库中创建的表格
        // create table datas(id int auto_increment,name varchar(500),age varchar(50),phone varchar(50),hobby varchar(500),descr varchar(500),primary key(id))

        // 6.拼接添加数据的SQL语句
        $sql = "insert into datas (name,age,phone,hobby,descr) values ('${n}','${a}','${p}','${h}','${d}')";
        // echo $sql;
        // 7.执行SQL语句
        $res = $mysqli->query($sql);
        // 返回为1表示成功 没值表示失败
        echo $res;
    }


?>